<template>
  <div class="modal-form-container">
    <vuescroll :ops="ops">
      <Form class="mr10 ml10" ref="info" :model="data" :label-width="100" :rules="rules">
        <FormItem :label="$t('ukexJfHe1YBGx8B9XnGdu')" prop="id">
          <Input v-model="data.id" :disabled="status != 'new'" />
        </FormItem>
        <FormItem :label="$t('VVZ9f2Owprgqy4vhNt4qT')" prop="sortindex">
          <Input v-model="data.sortindex" />
        </FormItem>
        <FormItem :label="$t('dUE9V6jUHBdS6m9avfiqp')" prop="name">
          <Input v-model="data.name" />
        </FormItem>
        <FormItem :label="$t('Pmmu2pvDykNb-WJ54KANR')">
          <Input v-model="data.manufacturername" />
        </FormItem>
        <FormItem :label="$t('o-XnhNowG-7eUc1KwjIAr')">
          <Input v-model="data.acdcmodelmanufactor" />
        </FormItem>
        <FormItem :label="$t('_U-ZtVG4JB3EY66WR98OJ')" prop="type">
          <Select v-model="data.type">
            <Option value="1">直流</Option>
            <Option value="2">交流</Option>
          </Select>
        </FormItem>
        <FormItem :label="$t('Hj_cOkH4-CUmh2cXEEaYZ')" prop="guncount">
          <Input v-model="data.guncount" />
        </FormItem>
        <FormItem :label="$t('1_Sb9giJznumi-BYcR49U') + '(kW)'" prop="capacity">
          <Input v-model="data.capacity" />
        </FormItem>
        <FormItem :label="$t('SxrSRhiwsCT-3jkAQnf7i') + '(V)'" prop="voltage">
          <Input v-model="data.voltage" />
        </FormItem>
        <FormItem :label="$t('q_bIDmxD6p4Pd4cuFXrYS') + '(A)'" prop="current">
          <Input v-model="data.current" />
        </FormItem>
        <FormItem :label="$t('kyhcIm8VUNbkfeKs0mMBi') + '(' + $t('7HETTJlktsKtCHcOB5fHN') + ')'" prop="swapelemeterbeforesumread">
          <Input v-model="data.swapelemeterbeforesumread" />
        </FormItem>
        <FormItem :label="$t('do3vwggpPql-Hd6Kvo0Hf')" prop="isforeign">
          <Select v-model="data.isforeign">
            <Option value="1">是</Option>
            <Option value="0">否</Option>
          </Select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="save">{{$t('q-FETLOZVSMbWnrNq0vj6')}}</Button>
          <Button style="margin-left: 8px" @click="close">{{$t('2LFpg56tAgKyBB40xpv5l')}}</Button>
        </FormItem>
      </Form>
    </vuescroll>
  </div>
</template>

<script>
import vuescroll from 'vuescroll'
import { addEquipment, updateEquipment } from '@/api/data2'

export default {
  name: 'pile_form_info',
  components: {
    vuescroll
  },
  props: {
    data: Object,
    status: String
  },
  data () {
    return {
      ops: {
        scrollPanel: {
          maxHeight: 500
        }
      },
      rules: {
        id: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        sortindex: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        name: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        type: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        guncount: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        capacity: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        voltage: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        current: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        swapelemeterbeforesumread: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }],
        isforeign: [{ required: true, message: this.$t('wXotjLC-TbA_OTLM9dES4'), trigger: 'blur' }]
      }
    }
  },
  methods: {
    close () {
      this.$emit('close')
    },
    transform () {
      return Object.assign({}, this.data, {
        type: this.data.type == 1 ? this.$t('1mJZgGX4yUf8keAuTCd6s') : this.$t('pUp8tS1uT4bIraonzAECH'),
        guncount: +this.data.guncount,
        capacity: +this.data.capacity,
        voltage: +this.data.voltage,
        current: +this.data.current,
        swapelemeterbeforesumread: +this.data.swapelemeterbeforesumread,
        sortindex: +this.data.sortindex,
        isforeign: +this.data.isforeign
      })
    },
    async save () {
      this.$refs.info.validate(async valid => {
        if (valid) {
          const data = this.transform()
          const { code, desc } =
            this.status === 'new'
              ? await addEquipment({ ...data })
              : await updateEquipment({ ...data })
          if (code == 200) {
            this.$emit('success', desc)
          } else {
            this.$emit('fail', desc)
          }
        }
      })
    }
  }
}
</script>
